package com.nsu.cl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nsu.cl.pojo.Achievements;
import com.nsu.cl.service.AchievementsService;
import com.nsu.cl.service.ClazzService;
import com.nsu.cl.service.GradeService;
import com.nsu.cl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "科目成绩控制器")
@RestController
@RequestMapping("/achievementsController")
public class AchievementsController {

    @Autowired
    private AchievementsService achievementsService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private GradeService gradeService;

    @ApiOperation("删除Achievements信息")
    @DeleteMapping("/deleteAchievements")
    public Result deleteAchievements(@ApiParam("要删除的所有的achievements的id的JSON集合") @RequestBody List<Integer> ids) {
        // 调用服务层
        achievementsService.removeByIds(ids);

        return Result.ok();
    }

    @ApiOperation("新增或修改Achievements，有id属性则是修改，没有则是增加")
    @PostMapping("/saveOrUpdateAchievements")
    public Result saveOrUpdateAchievements(@ApiParam("JSON的Achievements对象")@RequestBody Achievements achievements) {
        // 接收参数
        // 调用服务层方法完成增加或者修改
        achievementsService.saveOrUpdate(achievements);
        return Result.ok();
    }

    @ApiOperation("查询根据科目名称模糊查询，带分页")
    @GetMapping("/getAchievements/{pageNo}/{pageSize}")
    public Result getAchievements(@ApiParam("分页查询页码数")@PathVariable("pageNo") Integer pageNo,
                              @ApiParam("分页查询页大小")@PathVariable("pageSize")Integer pageSize,
                              @ApiParam("分页查询的条件")Achievements achievements) {
        // 分页 带条件查询
        Page<Achievements> page = new Page<>(pageNo,pageSize);
        // 通过服务层
        IPage<Achievements> iPage = achievementsService.getSubjectByOpr(page,achievements);

        // 封装Result对象并返回
        return Result.ok(iPage);
    }

    @ApiOperation("查询根据ID查询成绩信息")
    @GetMapping("/getAchievementsByid/{id}")
    public Result getAchievementsByid(@ApiParam("分页查询页码数")@PathVariable("id") Long id) {

         Achievements achievements = achievementsService.getSubjectById(id);

         achievements.setClazz(clazzService.getClazzByName(achievements.getStudent().getClazzName()));

         achievements.setGrade(gradeService.getById(achievements.getClazz().getGradeId()));

        return Result.ok(achievements);
    }
}
